package com.huangd.platform.common.utils;

import com.huangd.platform.modules.sys.entity.to.GamesTo;
import org.apache.commons.lang.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

public class ChannelUtils {

    /**
     * 根据游戏id获取渠道
     *
     * @param game
     * @return
     */
    public static List<String> getGameChannels(Integer game) {

        game = game == null ? 0 : game;
        List<GamesTo.GameTo> games = GamesTo.getGames();
        Integer finalGame = game;
        List<GamesTo.GameTo> list = games.stream().filter(gameTo -> gameTo.getId().equals(finalGame)).collect(Collectors.toList());
        return new ArrayList<>(Arrays.asList(list.get(0).getChannels().split(",")));
    }

    /**
     * 获取前所有包体
     *
     * @return
     */
    public static Map<Integer, String> getGameAll() {

        List<GamesTo.GameTo> games = GamesTo.getGames();
        Map<Integer, String> map = new HashMap<>();
        for (GamesTo.GameTo game : games) {
            map.put(game.getId(), game.getName());
        }

        return map;
    }

    /**
     * 根据渠道code获取渠道
     *
     * @param code
     * @return
     */
    public static List<String> getChannels(String code) {
        code = StringUtils.isBlank(code) ? "all" : code;
        List<String> channelList = new ArrayList<>();
        List<String> channelLists = new ArrayList<>();
        List<GamesTo.ChannelTo> channelToList = GamesTo.getChannels();
        String finalChannel = code;
        if ("ios".equals(code) || "android".equals(code)) {
            List<GamesTo.ChannelTo> channels = channelToList.stream().filter(channelTo -> channelTo.getCode().equals("all")).collect(Collectors.toList());
            List<String> list = Arrays.asList(channels.get(0).getChannels().split(","));

            channelLists = list.stream().filter(channelss -> {
                int type = "ios".equals(finalChannel) ? 1 : 2;
                return Integer.parseInt(channelss) / 1000 == type;
            }).collect(Collectors.toList());

            String join = DataUtils.join(channelLists, ",", "");

            channelList.add(join);

        } else {
            List<GamesTo.ChannelTo> channels = channelToList.stream().filter(channelTo -> channelTo.getCode().equals(finalChannel)).collect(Collectors.toList());
            channelList = channels.stream().map(GamesTo.ChannelTo::getChannels).collect(Collectors.toList());
        }
        return channelList;
    }
    /**
     * 根据渠道code, 包体获取渠道
     *
     * @param code
     * @return
     */
    public static List<String> getChannels(String code, Integer game) {
        List<String> channels = getChannels(code);
        List<String> list = new ArrayList<>(Arrays.asList(channels.get(0).split(",")));
        List<String> gameChannels = getGameChannels(game);
        gameChannels.retainAll(list);
        return gameChannels;
    }
    /**
     * 根据渠道code 获取包体
     *
     */
    public static Integer getChg(int channel) {
        List<GamesTo.GameTo> games = GamesTo.getGames();
        for (GamesTo.GameTo game : games) {
            String channels = game.getChannels();
            List<String> channelArr = Arrays.asList(channels.split(","));
            if (channelArr.contains(String.valueOf(channel))) {
                return game.getId();
            }
        }
        return -1;
    }

    public static Set<String> getChannelList(List<Integer> games) {
        Set<String> channels = new HashSet<>();
        for (Integer game : games) {
            channels.addAll(getGameChannels(game));
        }
        return channels;
    }

    public static Set<String> getChannelListByCode(List<String> sysytems) {
        Set<String> channels = new HashSet<>();
        for (String system : sysytems) {
            List<String> channels1 = getChannels(system);
            for (String channelStr : channels1) {
                String[] channelArr = channelStr.split(",");
                channels.addAll(Arrays.asList(channelArr));
            }
        }
        return channels;
    }


    public static Set<String> getChannes(List<Integer> games, List<String> sysytems) {

        Set<String> channelList = getChannelList(games);
        Set<String> channelListByCode = getChannelListByCode(sysytems);
        channelList.retainAll(channelListByCode);

        return channelList;
    }


}
